package fts.ftsui;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fts.ftscontrol.Facade;
import fts.ftsmonitoring.FileAction;
import fts.ftsmonitoring.IFileObserver;
import fts.ftsmonitoring.MonitoringStatus;


public class WatcherServlet extends HttpServlet implements IFileObserver
{
	static MonitoringStatus monStatus;
	Facade facade;
	
	public WatcherServlet(Facade facade)
	{
		this.facade = facade;
		this.facade.addFileObserver(this);
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		if("start".equals(req.getParameter("action")))
		{
			facade.startFileMonitoring();
		}
		else if("stop".equals(req.getParameter("action")))
		{
			facade.stopFileMonitoring();
		}
		
        String html = "<html><head><title>File Watcher</title></head><body>" 
        	+ "<h1>File Watcher</h1>"
        	+ "<a href=\"http://localhost:8181/watcher\">Refresh</a><br/><br/>"
        	+ "Status: " + monStatus + "<br/><br/>"
        	+ "<a href=\"http://localhost:8181/watcher?action=start\">Start</a><br/><br/>"
        	+ "<a href=\"http://localhost:8181/watcher?action=stop\">Stop</a><br/><br/>"
        	+ "</body></html>";
        resp.getWriter().println(html);
        resp.getWriter().flush();

	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	public void onFileChange(String srcFile, String destFile, FileAction action)
	{
		// TODO Auto-generated method stub
		
	}
	public void onMonitoringStatusChange(MonitoringStatus status)
	{
		monStatus = status;
	}
}
